package com.googlecode.messwertepi.gui;

import jcurses.system.CharColor;
import jcurses.system.Toolkit;
import jcurses.widgets.BorderPanel;

/**
 * Created by eike on 24.06.14.
 */
public class TitledBorderPanel extends BorderPanel
{
    final private static CharColor DEFAULT_TITLECOLOR = new CharColor(CharColor.WHITE, CharColor.BLACK);

    private String title;
    private CharColor titleColor;

    public TitledBorderPanel(String title, CharColor titleColor)
    {
        this.title = title;
        this.titleColor = titleColor;
    }

    public TitledBorderPanel(String title)
    {
        this(title, DEFAULT_TITLECOLOR);
    }


    public TitledBorderPanel(int width, int height, String title, CharColor titleColor)
    {
        super(width, height);

        this.title = title;
        this.titleColor = titleColor;
    }

    public TitledBorderPanel(int width, int height, String title)
    {
        this(width, height, title, DEFAULT_TITLECOLOR);
    }

    @Override
    protected void paintSelf()
    {
        super.paintSelf();
        drawTitle();
    }

    @Override
    protected void repaintSelf()
    {
        super.repaintSelf();
        drawTitle();
    }

    /**
     * Draws the title on this widgets border.
     * The title will always be drawn on the top left border of this widget.
     */
    private void drawTitle()
    {
        // shrink the title if it is to long
        int maximumWidth = getSize().getWidth() - 4;
        String text = title;
        if (text.length() > maximumWidth) {
            text = title.substring(0, maximumWidth);
        }

        // draw the title on the top left corner
        Toolkit.printString(text, getAbsoluteX() + (getSize().getWidth() - text.length()) / 2, getAbsoluteY(), titleColor);
    }
}
